
var Ajax = {

  _RET_NULL   : " ",
  _CONN_ERROR : "****",
  
  firstElement : function (elm){
  
    if(elm.length > 0){
    
      return elm[0];
    }else{
    
      return elm;
    }
  },
  
  /** 
	* Fetch the currency's rate
	*
	* @param HtmlObject         srcSymbol   the category selected
	* @param HtmlCollections    trDate      trDate
	* @param HtmlCollections    dstObject   the group selected
	* 
	*/
	getRate :function (srcSymbol, 
		                 trDate, 
		                 dstObject){
	  
	  trDate    = document.getElementsByName(trDate);
	  trDate    = trDate[0];
	  
	  dstObject = document.getElementsByName(dstObject);
	  dstObject = dstObject[0];
	  
	  if(srcSymbol.length > 0){
	    
	    var xmlHttp = new XMLHttpRequest();
	    
	    var url     = "/ecs/tocmenu_43/controllers/getRate.php" +"?sid="+Math.random();
	    var params  = "srcSymbol="    + srcSymbol +
	                  "&trDate="      + trDate.value ;
	    
	    xmlHttp.open("POST", url, true);
	    
	    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    xmlHttp.setRequestHeader("Content-length", params.length);
	    xmlHttp.setRequestHeader("Connection", "close");
	    
	    xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
	      
	      //alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
	      
	      if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
	        
	        switch(xmlHttp.responseText){
	          
	          case this._CONN_ERROR:
	            
	            alert("Db connection failed");
	            break;
	            
	          case this._RET_NULL:
	            
	            alert("Error has been occured");
	            break;
	            
	          default:
	            dstObject.value = xmlHttp.responseText;
	        }
	      }
	    }
	    
	    xmlHttp.send(params);
	  }
	},
  
  cvtQty : function(model, to, toQty, from, thisObj){
  
    model   = document.getElementsByName(model);
    to      = document.getElementsByName(to);
    toQty   = document.getElementsByName(toQty);
    from    = document.getElementsByName(from);
    
    model   = this.firstElement(model);
    to      = this.firstElement(to);
    toQty   = this.firstElement(toQty);
    from    = this.firstElement(from);
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_43/controllers/processConvertion.php" +"?sid="+Math.random();
    var params  = "model="  + model.value +
                  "&to="    + to.value    +
                  "&toQty=" + toQty.value +
                  "&from="  + from.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    
    switch(xmlHttp.responseText){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      default:
        thisObj.value = xmlHttp.responseText;
    }
  },
  
  unit  : function(model, from, thisObj){
  
    model = document.getElementsByName(model);
    
    if(model.length > 0){
    
      model = model[0];
    }
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_43/controllers/getUnit.php" +"?sid="+Math.random();
    var params  = "model=" + model.value +
                  "&from=" + from;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    
    switch(xmlHttp.responseText){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      default:
        thisObj.innerHTML = xmlHttp.responseText;
    }
  },
  
  model : function(isAll, thisObj){
  
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_12/controllers/getModel.php" +"?sid="+Math.random();
    var params  = "isAll="  + isAll +
                  "&model=" + thisObj.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    
    switch(xmlHttp.responseText){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      default:
        thisObj.innerHTML = xmlHttp.responseText;
    }
  },

  suppCstCode : function (suppCstCode,
                          suppCstEnt,
                          typeEnt){
    
    suppCstCode   = document.getElementsByName(suppCstCode);
    suppCstEnt    = document.getElementsByName(suppCstEnt);
    typeEnt       = document.getElementsByName(typeEnt);
    
    suppCstCode   = GMisc.firstElement(suppCstCode);
    suppCstEnt    = GMisc.firstElement(suppCstEnt);
    typeEnt       = GMisc.firstElement(typeEnt);
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_31/controllers/getSuppCustCode.php" +"?sid="+Math.random();
    var params  = "type="         + typeEnt.value +
                  "&suppCstName=" + suppCstEnt.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
      
    }
    
    xmlHttp.send(params);
    //alert(xmlHttp.responseText);
    suppCstCode.value = xmlHttp.responseText;
  },

  suppCst : function (suppCstEnt,
                      typeEnt){
    
    suppCstEnt  = document.getElementsByName(suppCstEnt);
    typeEnt     = document.getElementsByName(typeEnt);
    
    suppCstEnt  = GMisc.firstElement(suppCstEnt);
    typeEnt     = GMisc.firstElement(typeEnt);
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_31/controllers/getSuppCust.php" +"?sid="+Math.random();
    var params  = "type="    + typeEnt.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
      
    }
    
    xmlHttp.send(params);
    //alert(xmlHttp.responseText);
    suppCstEnt.innerHTML = xmlHttp.responseText;
  },

  crTerm : function (crTerm,
                     trDate,
                     dueDate){
                     
    crTerm  = document.getElementsByName(crTerm);
    trDate  = document.getElementsByName(trDate);
    dueDate = document.getElementsByName(dueDate);
    
    crTerm  = GMisc.firstElement(crTerm);
    trDate  = GMisc.firstElement(trDate);
    dueDate = GMisc.firstElement(dueDate);
    
    var url = "/ecs/tocmenu_43/controllers/getDateDifference.php";
    
    var params = "trDate="    + trDate.value  +
                 "&dueDate="  + dueDate.value ;
    
    var responseText = GAjax.SJax(url,
                                  params);
    //alert(responseText);
    switch(responseText){
            
      case this._CONN_ERROR:
        
        alert("Db connection failed");
        break;
        
      case this._RET_NULL:
        
        alert("Error has been occured");
        break;
        
      default:
        
        crTerm.value = responseText;
    }
  },

  setButton : function (tx,
                        customButton){
  
    tx            = document.getElementsByName(tx);
    customButton  = document.getElementsByName(customButton);
    
    tx            = GMisc.firstElement(tx);
    customButton  = GMisc.firstElement(customButton);
    
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_43/controllers/getButton.php" +"?sid="+Math.random();
    var params  = "theTx="    + tx.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
      
    }
    
    xmlHttp.send(params);
    //alert(xmlHttp.responseText);
    customButton.innerHTML = xmlHttp.responseText;
  },

  sayHello : function(){
  
    alert("Ajax Linked");
  }
} 